package com.edu.cdp.controller;

import com.edu.cdp.DTO.DetailDTO;
import com.edu.cdp.DTO.OrderDetailsDTO;
import com.edu.cdp.common.Result;
import com.edu.cdp.domain.OrderDetails;
import com.edu.cdp.service.OrderDetailsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/details")
@Slf4j
@Tag(name = "订单详情相关接口")
public class OrderDetailsController {
    @Autowired
    OrderDetailsService orderDetailsService;

    @GetMapping("/list")
    @Operation(summary = "获取所有商品详情")
    public Result<List<OrderDetails>> list() {
        List<OrderDetails> orderDetails = orderDetailsService.list();
        return Result.success(orderDetails);
    }

    @PutMapping("/update")
    @Operation(summary = "修改商品详情")
    public Result<OrderDetails> update(
            @RequestBody OrderDetailsDTO orderDetails) {
        OrderDetails orderDetail = orderDetailsService.update(orderDetails);

        if (orderDetail == null) {
            return Result.error("修改失败");
        } else {
            return Result.success();
        }
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除订单详情")
    public Result<String> delete(
            @Parameter(name = "id", description = "订单详情id")
            Integer id) {
        boolean b = orderDetailsService.removeById(id);
        if (b) {
            log.error("详情不存在");
            return Result.success();
        } else {
            return Result.error("删除失败");
        }
    }

    @PostMapping("/add")
    @Operation(summary = "添加订单详情")
    public Result<OrderDetails> add(
            @ParameterObject DetailDTO detailDTO) {
           OrderDetails orderDetail = orderDetailsService.add(detailDTO);

           if (orderDetail == null) {
               return Result.error("添加失败");
           } else {
               return Result.success(orderDetail);
           }
    }
}
